package com.example.demo.service;

import java.util.List;
import java.util.Random;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.bean.CmsLink;
import com.example.demo.mapper.CmsLinkMapper;

@Service
public class CmsLinkService {

	@Autowired
	private CmsLinkMapper cmsLinkMapper;
	
	
	public List<CmsLink> selectAll() {
		return cmsLinkMapper.selectAll();
	}
	
	public CmsLink selectById(Integer id) {
		return cmsLinkMapper.selectByPrimaryKey(id);
	}
	
	public int deleteById(Integer id)
	{
		CmsLink cmsLink =cmsLinkMapper.selectByPrimaryKey(id);
		
		if (cmsLink!=null) {
			return cmsLinkMapper.deleteByPrimaryKey(id);
		}
		return -1;
	}
	
	public int batchDeleteByIds(Integer[] ids) {
		if (ids!=null && ids.length>0) {
			return cmsLinkMapper.batchDeleteByIds(ids);
		}
		return 0;
	}
	
	public int saveOrUpdate(CmsLink cmsLink) {
		if (cmsLink.getId() !=null) {
			CmsLink cmsLink2= cmsLinkMapper.selectByPrimaryKey(cmsLink.getId());
			if (cmsLink2==null) {
				return -1;
			}
			cmsLinkMapper.updateByPrimaryKey(cmsLink);
			return 1;
		}
		else {
			Random  random =new Random();
			cmsLink.setId(random.nextInt(1000));
			cmsLinkMapper.insert(cmsLink);
		}
		
		return 1;
	}
}
